home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Documents / NeXTAnswers / objc.27 < prev    next >
Text File  |  1992-02-06  |  2KB  |  61 lines

  1. {\rtf0\ansi{\fonttbl\f0\fnil Times-Roman;\f2\fmodern Courier;\f3\fswiss Helvetica;}
  2. \paperw10880
  3. \paperh8420
  4. \margl120
  5. \margr120
  6. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\b0\i0\ul0\fs28 objc \
  7. \
  8. Q:  How does one mix C and Objective-C code?  How can one call Objective-C from C?\
  9. \
  10. A:  Just do it!  C functions can freely send Objective-C messages, and Objective-C methods can freely call C functions.   For an example, look at 
  11. \b main()
  12. \b0  of one of the applications in /NextDeveloper/Examples.   \
  13. \
  14. If you place Objective-C code in a ".c" file,  you need to compile the file with the Objective -C option.  The simplest way to do this is to change the ".c" suffix to ".m", and then add the file name under ".m (other)"  in IB's Project Inspector.    If it's not possible to change the suffix, compile the file using the 
  15. \b -ObjC 
  16. \b0 compiler flag.  \
  17. \
  18. You can place any C code in a ".m" file without any special handling.  The C code will need to extern id's as per regular C scoping rules.\
  19. \
  20. Only within Objective-C methods can you directly access instance variables and the hidden variable "self."  You must pass an object to a C function that needs access to it.   For example, this C function will return the width of a View:\
  21. \
  22.  
  23. \f2\fs24 NXCoord widthOfView(View *self)\
  24. \{\
  25.     NXRect b;\
  26.     [self getBounds:&b];\
  27.     return b.size.width;\
  28. \}\
  29.  
  30. \f0\fs28 \
  31. You may access 
  32. \b public
  33. \b0  instance variables like this: \
  34. \
  35.  
  36. \f2\fs24 void setNumber(MyObject *self,int newValue)\
  37. \{\
  38.     self->number = newValue;\
  39. \}\
  40.  
  41. \f0\fs28 \
  42. Remember that all instance variables are considered private unless explicitly declared public.  However, within the confines of a class implementation, all instance variables of that class are considered public.  So in the implementation for MyControl (a subclass of Control), you could write a C function to set the tag of a MyControl object like this:\
  43. \
  44.  
  45. \f2\fs24 void setTag(MyControl *self,int newTag)\
  46. \{\
  47.     self->tag = newTag;\
  48. \}\
  49. \
  50. \
  51.  
  52. \f0\fs28 QA27\
  53.  
  54. \pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320\f3\fs24 \
  55.  
  56. \f0\fs28 Valid for 1.0\
  57. Valid for 2.0\
  58.  
  59. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600 \
  60.  
  61.